//
//  NewsViewController.m
//  Movie
//
//  Created by tengyang on 15/7/17.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsModal.h"
#import "NewsCell.h"
#import "DataService.h"
#import "UIImageView+WebCache.h"
#import "UIViewExt.h"
#import "ViewController1.h"
#import "NewsdetailViewController.h"

#import <MediaPlayer/MediaPlayer.h>

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface NewsViewController ()
{
    UIImageView *_headerImageView;
    UILabel *_headerLable;
}
@end

@implementation NewsViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"新闻";
    }
    return self;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadJson];
    [self _createTableView];
    [self _createHeaderView];



}

- (void)_loadJson
{

    NSArray *array=[DataService getJsonDataFromFile:@"news_list.json"];
    _newsModalArray=[[NSMutableArray alloc]init];
    
    for (NSDictionary *dic in array) {
        NewsModal *modal=[[NewsModal alloc]init];
        modal.title=[dic objectForKey:@"title"];
        modal.summary=[dic objectForKey:@"summary"];
        modal.image=[dic objectForKey:@"image"];
        NSString *temp=[dic objectForKey:@"type"];
        modal.type=temp.integerValue;
        
        [_newsModalArray addObject:modal];
    }
    
    
        
}

- (void)_createTableView
{
    _newsTableView=[[UITableView alloc]initWithFrame:self.view.bounds];
    _newsTableView.dataSource=self;
    _newsTableView.delegate=self;
   
    
    //设置了tableview的头视图
//    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];
//    view.backgroundColor=[UIColor redColor];
//    _newsTableView.tableHeaderView=view;
    
    
    [self.view addSubview:_newsTableView];
    
    UINib *nib=[UINib nibWithNibName:@"NewsCell" bundle:[NSBundle mainBundle]];
    
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"newscell"];
    
}


//实现了下拉放大的功能
- (void)_createHeaderView
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    view.backgroundColor=[UIColor clearColor];
    _newsTableView.tableHeaderView=view;
    
    _headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, kWidth, 200)];
    NewsModal *modal=[[NewsModal alloc]init];
    modal=_newsModalArray[0];
    _headerLable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,kWidth, 20)];
    
    _headerLable.text=modal.title;
    _headerLable.textColor=[UIColor whiteColor];
    _headerLable.backgroundColor=[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.7];
    

    
    NSString *temp=modal.image;
    [_headerImageView sd_setImageWithURL:[NSURL URLWithString:temp]];
    _headerLable.bottom=_headerImageView.bottom;
    
    [self.view addSubview:_headerImageView];
    [self.view addSubview:_headerLable];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offY= scrollView.contentOffset.y;
    if (offY>-64) {
        _headerImageView.top=-offY;
        _headerLable.bottom=_headerImageView.bottom;
    }
    else
    {
        CGFloat newHeight=ABS(offY)-64+200;
        CGFloat newWidth=kWidth/200 *newHeight;
        
        _headerImageView.frame=CGRectMake((kWidth-newWidth)/2, 64, newWidth, newHeight);
        _headerLable.bottom=_headerImageView.bottom;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _newsModalArray.count-1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"newscell" forIndexPath:indexPath];
    
    
    NewsModal *modal=_newsModalArray[indexPath.row+1];
    cell.modal=modal;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModal *modal=_newsModalArray[indexPath.row+1];
    if (modal.type==1) {
        ViewController1 *vc1=[[ViewController1 alloc]init];
        [self.navigationController pushViewController:vc1 animated:YES];

    }
    if (modal.type==0) {
        NewsdetailViewController *vc=[[NewsdetailViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    if (modal.type==2) {
        MPMoviePlayerViewController *ctrl = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4"]];
        
   [self presentMoviePlayerViewControllerAnimated:ctrl];
    }
    
}



@end
